import React, { Component } from 'react'

export default class VoteBody extends Component {
    constructor(props) {
        super(props)
        const { store } = this.props
        const { suppNum, oppNum } = store.getState()
        this.state = {
            suppNum,
            oppNum
        }
    }
    componentDidMount() {
        const { store } = this.props
        // 订阅事件
        store.subscribe(() => {
            console.log(store.getState())// {title: 'React vs Vue 哪家强？', suppNum: 0, oppNum: 0 }
            const { suppNum, oppNum } = store.getState()
            // 自己做个渲染优化吧
            if (suppNum !== this.state.suppNum) {
                this.setState({
                    suppNum: suppNum
                })
            }
            if (oppNum !== this.state.oppNum) {
                this.setState({
                    oppNum: oppNum
                })
            }
        })

    }
    render() {
        const { suppNum, oppNum } = this.state
        return (
            <div>
                <p>suppNum:{suppNum}</p>
                <p>oppNum:{oppNum}</p>
            </div>
        )
    }
}
